package com.haipumi.actions;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.haipumi.beans.GoodsModel;
import com.haipumi.beans.JsonModel;
import com.haipumi.beans.haipumi_product;
import com.haipumi.beans.haipumi_user;
import com.haipumi.biz.ExchangeListBiz;
import com.haipumi.biz.GoodsBiz;
import com.haipumi.biz.TRewardBiz;
import com.haipumi.utils.CommonData;
import com.opensymphony.xwork2.ActionSupport;

public class GoodsAction extends ActionSupport{

	private static final long serialVersionUID = -8289947299319394784L;
	private JsonModel jm;
	private HttpServletRequest request=ServletActionContext.getRequest();
	private String op;
	
	@Autowired
	private TRewardBiz tRewardBiz;
	
	@Autowired
	private ExchangeListBiz exchangeListBizImpl;
	
	@Autowired
	private GoodsBiz goodsBiz;
	
	public JsonModel getJm() {
		return jm;
	}

	public void setJm(JsonModel jm) {
		this.jm = jm;
	}

	public void setOp(String op) {
		this.op = op;
	}
	
	@Override
	public String execute() {
		if(op!=null){
			if("findAll".equals(op)){
				findAllOp( );
			}else if("payForIt".equals(op)){
				payForIt( );
			}else if("findRecord".equals(op)){
				findRecord( );
			}
		}
		return "success";
	}

	private void findRecord() {
		String elid=((haipumi_user)request.getSession().getAttribute(CommonData.USERSESSION)).getUid();
		List<GoodsModel> recordlist= null ;
		try {
			 recordlist=goodsBiz.findRecord(elid);
		} catch (Exception e) {
			e.printStackTrace();
			jm.setCode(0);
			jm.setMsg("查询错误，请联系管理员");
		}
		jm=new JsonModel();
		jm.setCode(1);
		jm.setObj(recordlist);
	}

	private void findAllOp() {
		jm=new JsonModel();
		List<haipumi_product> list=(List<haipumi_product>) request.getSession().getServletContext().getAttribute(CommonData.APPLICATIONPRODUCTKEY);
		jm.setCode(1);
		jm.setObj(list);
	}
	
	private void payForIt(){
		jm=new JsonModel();
		haipumi_user user=(haipumi_user) request.getSession().getAttribute(CommonData.USERSESSION);
		String pid=request.getParameter("pid");
		//TODO:从用户表扣除相应积分：折扣*所需积分数       添加一条兑换记录到exchangelist
		haipumi_product good=goodsBiz.findGoodByPid(pid);
		if(Integer.parseInt(good.getPnum())<1){
			//没有商品，不能兑换
			jm.setCode(0);
			jm.setMsg("商品库存不足，请稍后尝试");
			return ;
		}
		int uwealth=Integer.parseInt(user.getUwealth());
		int pneed=(int) (Double.parseDouble(good.getPdiscount())*Integer.parseInt(good.getPneed()));
		if(uwealth<pneed){
			//用户积分不足
			jm.setCode(0);
			jm.setMsg("您的积分不足，多多帮助他人，拿积分吧~！");
			return ;
		}
		//可以兑换
		//1.扣除积分
		tRewardBiz.payReward(user.getUid(), "-"+Integer.toString(pneed));
		haipumi_user backuser=(haipumi_user) request.getSession().getAttribute(CommonData.USERSESSION);
		String uwealths=Integer.toString(Integer.parseInt(backuser.getUwealth())+Integer.parseInt("-"+Integer.toString(pneed)));
		backuser.setUwealth(uwealths);
		request.getSession().setAttribute(CommonData.USERSESSION, backuser);
		
		//2.添加兑换记录
		exchangeListBizImpl.insertExchange(user.getUid(),good.getPid(), "0","1");
		jm.setCode(1);
		jm.setMsg("兑换成功");
		jm.setObj(uwealths);
	}

}
